﻿using Sunny.UI;
using System;
using System.Collections.Concurrent;
using System.Windows.Forms;
using EventMgrLib;
using SmartMotion.Core;
using SmartMotion.Product;
using SmartMotion.Properties;

namespace SmartMotion.AllPages
{
    public partial class FrmProductMgr : UIPage
    {
        private ConcurrentDictionary<string, VarInfo> _vars;
        private EventMgr _em;
        private ProductMgr _pm;
        public FrmProductMgr()
        {
            InitializeComponent();
        }

        private void FrmProductMgr_Load(object sender, EventArgs e)
        {
            
            _vars = GlobalVars.Ins.Vars[GlobalVars.ProductPars];
            _em=EventMgr.Ins;
            _pm=ProductMgr.Ins;
            _em.GetEvent<ProductEvent>().Subscribe(ProductOpt);

            SetGrid();
            RefreshData();

        }

        private void ProductOpt(FileOpt fileOpt)
        {
          
                switch (fileOpt)
                {
                    case FileOpt.None:
                        break;
                    case FileOpt.Create:
                        _pm.New();
                        break;
                    case FileOpt.Open:
                        _pm.Open();
                        _vars = GlobalVars.Ins.Vars[GlobalVars.ProductPars];
                        RefreshData();
                        SavePath();
                        break;
                    case FileOpt.Save:
                        _pm.Save();
                        SavePath();
                        break;
                    case FileOpt.SaveAs:
                        _pm.SaveAs();
                        SavePath();
                        break;
                    case FileOpt.Add:
                        var frmParAdd = new frmParAdd();
                        if (frmParAdd.ShowDialog() == DialogResult.OK)
                        {
                            if (_vars.ContainsKey(frmParAdd.Result.Name))
                            {
                                ShowErrorDialog("参数已存在");
                            }
                            else
                            {
                                _vars.TryAdd(frmParAdd.Result.Name, frmParAdd.Result);
                                RefreshData();
                            }
                        }
                        break;
                    case FileOpt.Del:
                        var dataGridViewRow = this.dgv.CurrentRow;
                        if (dataGridViewRow != null)
                        {
                            var par = dataGridViewRow.DataBoundItem as VarInfo;
                            if (par == null)
                            {
                                return;
                            }

                            _vars.TryRemove(par.Name, out _);   
                        }

                        RefreshData();
                        break;
                    case FileOpt.Edit:
                        break;
                }
         
        }

        void SetGrid()
        {
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgv.RowHeadersVisible = false;
            dgv.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgv.AllowUserToResizeRows = false;
            dgv.ReadOnly = true;
            dgv.MultiSelect = false;
            dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        void RefreshData()
        {
            var bs = new BindingSource();
            bs.DataSource = _vars.Values;
            dgv.DataSource = bs;

        }

        private void SavePath()
        {
            Settings.Default.ProductFilePath = _pm.FilePath;
            Settings.Default.Save();
            
        }
    }
}
